/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package IncomeExpense;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Shailesh
 */
public class AddIncomeExpense extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            int k = 0;
            IncomeExpenseBean ib = new IncomeExpenseBean();
            ib.setAccountType(Integer.parseInt(request.getParameter("account_type")));
            ib.setAmount(Integer.parseInt(request.getParameter("amount")));
            ib.setDescription(request.getParameter("narrative"));
            ib.setPaymode(Integer.parseInt(request.getParameter("pay_mode")));
            ib.setChequeno(request.getParameter("cheque_no"));
            ib.setBank(request.getParameter("bank"));
            ib.setBranch(request.getParameter("branch"));
            String date[] = request.getParameter("dot").split("/");
            if (date.length == 3) {
                Date d = new Date(Integer.parseInt(date[2]) - 1900, Integer.parseInt(date[0]) - 1, Integer.parseInt(date[1]));
                ib.setDOT(d);
            }
            ib.setUserId(Integer.parseInt(session.getAttribute("uid").toString()));
            k = IncomeExpense.Query.add(ib);
            if (k != 0) {
                if (ib.getAccountType() == 1) {
                    session.setAttribute("status", "Success");
                    session.setAttribute("msg", "Income of (" + ib.getAmount() + ") Added Successfully.");
                    response.sendRedirect("/RIT/Services/Accounts/AddIncomeExpense.jsp");
                } else {
                    session.setAttribute("status", "Success");
                    session.setAttribute("msg", "Expense of (" + ib.getAmount() + ") Added Successfully.");
                    response.sendRedirect("/RIT/Services/Accounts/AddIncomeExpense.jsp");
                }

            } else {
                session.setAttribute("status", "Fail");
                session.setAttribute("msg", "Check Income Expense Details and try again.");
                response.sendRedirect("/RIT/Services/Accounts/AddIncomeExpense.jsp");
            }
        } catch (Exception e) {
            out.println(e);
            //response.sendRedirect("/RIT/Services/Accounts/AddIncomeExpense.jsp (e)");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AddIncomeExpense.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(AddIncomeExpense.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
